<?php
namespace logic\sys\message;
use \logic\Base;
class Message{
    
    private $callUser;  // 调用发送信息的对象
    public function __construct(\logic\user\User $callUser){
        $this->callUser = $callUser;
    }    
    
    /**
     * 发送信息
     * @param $messageTpl 信息模板标识
     * @param $message  信息模板的变量内容 array => array( code => 1234 , shop => 店铺名称)
     * @param $sendee 接收信息的人 目前只支持 手机号码
     * @param $limitTime 同一个手机号码发送短信的最少时间间隔(秒)
     */
    public function send($messageTpl,array $message,$sendee ,$limitTime = 60){
        if( !\core\helper\Verify::check($sendee,'mobile') )  return array( 'ret' => false, 'msg' => '手机号码格式错误');        
        //$checkRet = $this->checkSendLimitTime($sendee,$limitTime);
        //if(!$checkRet['ret']) return $checkRet;
        $MessageTpl = new \logic\sys\message\MessageTpl();
        $tpl = $MessageTpl->getTpl($messageTpl);
        if( !$tpl ) return array( 'ret' => false, 'msg' => '短信模板未配置');
        // 校验模板变量
        $error = '';
        foreach( $tpl['config'] as $key => $val ){
            if( !isset($message[$val]) ) $error = $error.$val.',';
        }
        if( $error ){
            $error = rtrim($error,',').'短信变量不能为空';
            return array( 'ret' => false, 'msg' => $error );
        }  
        $sendContent = $this->Parsing($tpl,$message);
        
        /*
        if( config('runtime') !== 'production' ){
            $this->saveSendLog($sendContent,true,$sendee,'测试站不实际发送短信',1);
            return ['ret' => true, 'msg' => 'ok'];
        }*/
        // 开始发送短信
        /*
        $Tencent = new \logic\sys\message\mobile\Tencent();
        $result  = $Tencent->send($sendee,$sendContent);
        dump($result);exit;
        */
        $MessageJhMobile = new \logic\sys\message\mobile\MessageJh();
        $result = $MessageJhMobile->send($sendee,$tpl['tplid'],$message);
        $this->saveSendLog($sendContent,$result['ret'],$sendee,$result['msg'],1);
        if( !$result['ret']){            
            // 聚合发送失败 调用 雷雨科技 发送短信            
            $MessageMobile = new \logic\sys\message\mobile\Message();
            $result = $MessageMobile->send( $sendee,$sendContent );
            $this->saveSendLog($sendContent,$result['ret'],$sendee,$result['msg'],2);
        }
        return $result;
    }
    
    /**
     * 短信时间间隔校验
     */
    private function checkSendLimitTime($sendee,$time){
        $time = intval($time);
        if( $time < 1 ) return ['ret' => true,'msg' => 'ok'];
        $CaseRedis =  new \logic\sys\redis\server\CaseRedis('string');
        $key = 'logic.message.sendlock'.$sendee;
        $history =  $CaseRedis->get($key);
        if( $history ) return ['ret' => false,'msg' => '短信发送过于频繁'];
        $CaseRedis->save($key,time(),$time);
        return ['ret' => true,'msg' => 'ok'];
    }
    
    /**
     * 对信息模板变量解析替换
     */
    private function Parsing($messageTpl,$message){
        $messageResult = $messageTpl['message'];
        foreach($messageTpl['config'] as $val ){
            if( isset($message[$val]) ) $messageResult = str_replace('{{'.$val.'}}',$message[$val],$messageResult);
        }
        return $messageResult;
    }
    
    
    /**
     * 记录发送信息的日志
     * @param $message 短信内容
     * @param $status 发送状态 1 成功 2 失败
     * @param $sendee 短信接收人
     * @param $returnData 返回的信息
     * @param $type 发送短信的类型 1 聚合数据 2 雷雨科技
     */
    private function saveSendLog($message,$status,$sendee,$returnData,$type){
        return true;
        $MongoDb =  \logic\sys\mongo\MongoDb::instance('message'); 
        $data = array(
            'message'   => $message,
            'status'    => $status ? 'success' : 'fail',
            'sendee'    => $sendee,
            'data'      => $returnData,
            'type'      => $type,
            'addtime'   => time()
        );
        $result = $MongoDb->insert($data);
    }
    
    
}